window.ROOT = "";
(function() {
	//初始化增删改操作权限
	/*var isAccess=$("#userRoleInfo").text()=="审核大队"?true:false;
	if(!isAccess){
		$("#checkAddBtn").parent().empty();	
	}*/
	var serverData = {};
	var serverTable;
	jQuery(document).ready(function() {
	serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/bussinessCheck/page',
				"data" : function(d) {
					return $.extend({}, d,
							serverData);
				}
			},
			columns : [
						{
							data : null,
							defaultContent : ""
						},
						 {
		                    data: null,
		                    defaultContent: "",
		                    render: function (data, type, full, meta) {
		                        var operate ='<button type="button" class="btn btn-sm btn-info searchBtn" title="查看详情"><i class="fa fa-search"></i></button>';
		                        return operate;

		                    }
		                },
						{
							data : 'businesssellerBaseInfo.shopName'
						},
						{
							data : 'businesssellerBaseInfo.shopAdress'
						},
					
						{
							data : 'businesssellerBaseInfo.managementRange'
						},
						
						{
							data : 'businesssellerBaseInfo.managementPerson.name'
						},
						{
							data : 'checkCondition'
						},
						
						{
							data : 'user.name'
						},
						{
							data : 'user.contactType'
						},

						{
							data : 'checkDate',
							render: function (data, type, full, meta) {
		                        if (data)
		                            return moment(data).format('YYYY/MM/DD HH:mm');
		                        else
		                            return '';
		                    }
						},
						{
							data : 'remark'
						},
					/*	{
							data : null,
							defaultContent : "",
							render : function(data,
									type, full,
									meta) {
								var operate ="";
								if(isAccess){
									operate='<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
										+ '<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';
								}
								return operate;

							}
						}*/ ],
			"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length
						+ oSettings._iDisplayStart;

				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
					k++;
				}

				serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = serverTable.row(this).data();
	                    if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
	                        showDetail(data);
	                    }

	                    return;
                });
			}
		});
	
	$("#exportToExcel").click(function(){
		$("#test").attr("src","");
		var key=$("#checkSearch").val();
		var url = rootPath+"/ui/bussinessCheck/exportToExcel?key="+key;
		$("#test").attr("src",url);
	});
	
	//查看核检详情
	var showDetail=function(data){
     	if(!data) return;
     	
     	var picUrl = data.pictureAddr;
 		if(picUrl==undefined||picUrl==""){
 			picUrl=window.rootPath + "/img/nopicture.png";
		}else{
			picUrl=window.rootPath+'/'+picUrl;
		}
     	
     	jQuery('#shopName').html(data.businesssellerBaseInfo.shopName);
     	jQuery('#checkCondition').html(data.checkCondition);
     	jQuery('#rectifyCondition').html(data.rectifyCondition);
     	jQuery('#userName').html(data.user.name);
     	jQuery('#userContactType').html(data.user.contactType);
     	jQuery('#checkDate').html(moment(data.checkDate).format('YYYY-MM-DD HH:mm'));
     	jQuery('#shopAdress').html(data.businesssellerBaseInfo.shopAdress);
     	jQuery('#managementRange').html(data.businesssellerBaseInfo.managementRange);
     	jQuery('#remark').html(data.remark);
     	jQuery('#avatar').attr("src",picUrl);
		var details = data.details;
		$("#details tr:not(:first)").empty();
		$.each(details, function (i) {
			var picAddr = this.pictureAdress;
	 		if(picAddr==undefined||picAddr==""){
	 			picAddr=window.rootPath + "/img/nopicture.png";
			}else{
				picAddr=window.rootPath + picAddr;
			}
			var detailsTab = $("#details");
			var html = '<tr><td>'+(i+1)+'</td><td><img src="'+picAddr+'" width="50px" height="50px"/></td>'
					+ '<td>' + this.repairInfo + '</td>'
					+ '<td>' + this.memo + '</td><td>'+this.checkTypeInfo.title + '</td></tr>';
			detailsTab.append(html);
			
		})
     	jQuery('#checkModal').modal('show');
     }
	
	//删除该条巡检,使用的是restful风格的DELETE提交方式
	function deleteCheck(data){
		if(confirm('确定删除该条巡检记录?')){
			$.ajax({
				url:window.rootPath+'/ui/bussinessCheck/delete/'+data.id,
				dataType:'json',
				type:'post',
				data:'_method=DELETE',
				success:function(result){
					//返回成功
					if(result.code==1){
						window.location=window.rootPath+'/ui/bussinessCheck';
					}
				}
			});
		}
	};

	//RESTFUL风格的GET方式去修改页面
	function editCheck(data){
		if(confirm('确定修改该条巡检记录?')){
			//修改页面
			window.location=window.rootPath+'/ui/bussinessCheck/saveOrUpdate/'+data.id;
		}
	};
	
	//条件检索
	$(".searchBtn").click(function(){
		serverData.key = jQuery("#checkSearch").val();
		serverTable.draw();
	});
	//撤销查询
    jQuery('.resetBtn').on('click',function(){
        jQuery('#checkSearch').val('');
        serverData.key='';
        serverTable.draw();
    });
	/*//添加数据
	$("#checkAddBtn").click(function(){
		//添加页面,使用get方式不带Id
	
		window.location.href=window.rootPath+"/ui/bussinessCheck/saveOrUpdate";
	});*/

	

});})();